﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.SysManage.RoleManage
{
    public partial class RoleManage : System.Web.UI.Page
    {
        RoleManageManage manage = new RoleManageManage();

        public void dataBind()
        {
            rpRole.DataSource = manage.getAllRoleInfo();
            rpRole.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            
            if (!IsPostBack)
            {
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;

                }
                if (Request.QueryString["NodeValuePath"] == null)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                int nodeId;
                if (int.TryParse(Request.QueryString["NodeValuePath"], out nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                string loginId = (string)Session["loginId"];
                if (new CommandManage().isHaveRoleRight(loginId, nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }

                dataBind();
                btnUpdate.Enabled = false;
            }
           


        }

        /// <summary>
        /// repeater控件里的删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageDelete_Click(object sender, ImageClickEventArgs e)
        {
            //获取前台绑定的ID
            int id = int.Parse(((ImageButton)sender).CommandName);

            if (manage.deleteRoleInfoByRoleId(id))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除成功',null,null,'温馨提示',nameFocus)</script>", false);
                dataBind();
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('此角色不能删除，真的要删除此角色请与技术人员联系',null,null,'温馨提示',nameFocus)</script>", false);
            }

            txtRoleName.Text = "";
            hfdRoleId.Value = "";

        }
        /// <summary>
        /// repeater控件里的修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageUpdate_Click(object sender, ImageClickEventArgs e)
        {
            //获取前台绑定的ID
            int roleId = int.Parse(((ImageButton)sender).CommandName);
            //获取前台绑定的角色名
            string roleName = ((ImageButton)sender).CommandArgument;
            txtRoleName.Text = roleName;
            btnSave.Enabled = false;
            btnUpdate.Enabled = true;

            //给前台隐藏的控件赋值
            hfdRoleId.Value = roleId.ToString();
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (manage.insertRoleInfo(txtRoleName.Text))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('添加成功',null,null,'温馨提示',nameFocus)</script>", false);
                dataBind();
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('添加失败',null,null,'温馨提示',nameFocus)</script>", false);
            }
            txtRoleName.Text = "";
            hfdRoleId.Value = "";
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (hfdRoleId.Value != string.Empty)
            {
                int roleId = int.Parse(hfdRoleId.Value);
                string roleName = txtRoleName.Text;
                RoleInfo roleInfo = new RoleInfo();
                roleInfo.RoleName = roleName;
                roleInfo.RoleId = roleId;

                if (manage.updateRoleInfoByRoleId(roleInfo))
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('修改成功',null,null,'温馨提示',nameFocus)</script>", false);
                    dataBind();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('修改失败',null,null,'温馨提示',nameFocus)</script>", false);
                }
                txtRoleName.Text = "";
                hfdRoleId.Value = "";
            }

        }
    }
}
